package com.justgame.project.service.system.controller;

import com.justgame.project.base.annotation.ProtectAccess;
import com.justgame.project.base.annotation.UploadApi;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.enums.FileSizeEnum;
import com.justgame.project.cloud.common.enums.FileTypeEnum;
import com.justgame.project.cloud.common.util.FileUtils;
import com.justgame.project.cloud.common.util.ShiroUtils;
import com.justgame.project.service.api.dto.OnlineUserInfoDTO;
import com.justgame.project.service.api.dto.SysFileInfoDTO;
import com.justgame.project.service.api.entity.SysFileInfo;
import com.justgame.project.service.api.feign.IOnlineUserClient;
import com.justgame.project.service.system.service.SysFileInfoService;
import com.justgame.project.service.system.service.SysUserInfoService;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/f-info")
@AllArgsConstructor
public class SysFileInfoController {
    private final SysFileInfoService fileInfoService;
    private final SysUserInfoService sysUserInfoService;
    private final IOnlineUserClient onlineUserClient;
    /**
     * 获取我的文件
     *
     * @return
     */
    @GetMapping("/all-mine")
    public Result<?> getMineFiles() {
        return Result.OK(fileInfoService.getMineFiles());
    }


    /**
     * 通用文件删除
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/delete")
    @ProtectAccess
    public Result<?> deleteFile(SysFileInfoDTO dto) {
        return Result.status(fileInfoService.deleteFile(dto));
    }

    /**
     * Feign接口 —— 【删除文件】
     *
     * @param dto
     * @return
     */
    @PostMapping("/real-delete")
    @ProtectAccess(type = {ProtectAccess.ProtectType.Feign})
    public Result<?> realDelete(@RequestBody SysFileInfoDTO dto) {
        return Result.status(fileInfoService.realDelete(dto));
    }

    @ResponseBody
    @PostMapping(value = "/upload")
    @RequiresRoles("admin")
    @UploadApi
    public Result<Object> uploadFile(@RequestParam("file") MultipartFile[] uploadFiles) {
        FileUtils fileUtils = new FileUtils();
        List<Map<String, Object>> files =
                fileUtils.AliYunOssUpload(
                        uploadFiles,
                        FileTypeEnum.System,
                        1,
                        FileSizeEnum.MB.getSize(20),
                        false
                );
        List<String> paths = fileUtils.paths;
        fileInfoService.save(new SysFileInfo(), files);
        return Result.OK("上传成功", paths.get(0));
    }


    /**
     * 上传图片
     */
    @ResponseBody
    @PostMapping(value = "/uploadImage")
    @ProtectAccess( value = 1L, frequencyType = ProtectAccess.Time.DAY, message = "一小时内只能修改一次头像哦，请稍后再试吧！")
    public Result<Object> uploadImage(@RequestParam("image") MultipartFile[] uploadFiles) {
        String userId = ShiroUtils.getUserId();
        FileUtils fileUtils = new FileUtils();
        List<Map<String, Object>> files =
                fileUtils.AliYunOssUpload(
                        uploadFiles,
                        FileTypeEnum.Image,
                        1,
                        FileSizeEnum.MB.getSize(3),
                        true
                );
        List<String> paths = fileUtils.paths;
        OnlineUserInfoDTO dto = new OnlineUserInfoDTO();
        dto.setUserId(userId);
        dto.setImages(paths.get(0));
        onlineUserClient.updateUserImage(dto);
        fileInfoService.save(new SysFileInfo(), files);
        return Result.OK("上传成功", paths.get(0));
    }

    ///**
    // * 上传音频
    // */
    //@ResponseBody
    //@PostMapping(value = "/uploadVideo")
    //@UploadApi
    //public Result<Object> uploadVideo(@RequestParam("video") MultipartFile[] uploadFiles) {
    //    FileUtils fileUtils = new FileUtils();
    //    List<Map<String, Object>> files =
    //            fileUtils.saveFiles(
    //                    uploadFiles,
    //                    FileTypeEnum.Video,
    //                    1,
    //                    FileSizeEnum.MB.getSize(10)
    //            );
    //    List<String> paths = fileUtils.paths;
    //    fileInfoService.save(new SysFileInfo(), files);
    //    return Result.OK("上传成功", paths.get(0));
    //}

}
